/**
 * 文章属性编辑
 */
define( [
  'jquery',
  'require',
  'bigwe_utils',
  '../../lib/editData',
  '../../modules/autoSave',
  '../../lib/utils',
  '../../modules/createEditor',
  'api',
  'combiner',
  'article_import',
  'ueditor',
  'message',
  'bigwe_component'
], function( $, require, bu ) {
  var ue = require( '../../modules/createEditor' );
  var editData = require( '../../lib/editData' );
  var AutoSave = require( '../../modules/autoSave' );
  var BIGWE_API = require( 'api' );
  var combiner = require( 'combiner' );
  var ai = require( 'article_import' );
  var msg = require( 'message' );
  var bc = require( 'bigwe_component' );

  var showTime = msg.showTime;

  $( document ).on( 'click', '#WxMsgContent .js_synonymsDialog', function() {

    ue.ui._dialogs[ 'synonymsDialog' ].open();

  } ).on( 'click', '#WxMsgContent .js_article_import', function() {
    ai.$selector.modal( 'show' );
  } ).on( 'click', '.js_clear_art_content', function() {

    ue.getContent() && bc.deleteConfirmShowBottom( {
      text: '确认要清空当前正文吗？',
      obj: this,
      fun: function() {
        ue.execCommand( 'cleardoc' )
      }
    } );

  } );

  var wxclient = new ZeroClipboard( $( '.js_copy_art_content' ) );
  wxclient.on( 'ready', function( event ) {
    wxclient.on( 'copy', function( event ) {
      var html = ue.getContent();
      event.clipboardData.setData( 'text/html', html );
      msg.show( '正文复制成功！', showTime );
    } );
  } );

  $( '#global-zeroclipboard-html-bridge' ).tooltip( {
    trigger: 'manual'
  } ).hover( function() {
    var _this = this;
    setTimeout( function() {

    	var title = $( _this ).prop( 'title' );
      if ( title === '复制正文' ) {
      	$( _this ).attr( 'data-original-title', title ).removeAttr( 'title' ).tooltip( 'show' );
      }

    } );
  }, function() {
    $( this ).tooltip( 'hide' );
  } );

  ue.ready( function() {

    if ( editData.allIsReady ) {
      //$('.graph-manage>.comb-list>li:first-child .js_writing').trigger('click');
      // 编辑图文时设置没有动过内容
      if ( !editData.isCreate ) {
        require( '../../lib/utils' ).changeFalseSet();
      }
    }
    // 判断是否有草稿
    AutoSave.showDraftLink();
    $( '.edit-column2>.editor-load' ).hide();
    $( '#WxMsgContent' ).show();

    // 标题提示隐藏
    $( ue.body ).click( function() {
      $( '#WxMsgContent .prompt' ).hide();
    } );
  } );
  ue.addListener( 'focus', function( e ) {
    $( '.edui-toolbar>.toolbarbox-wrap' ).hide();
  } );

  ue.addListener( 'mousedown', function( e, a ) {
    $( '#synonym' ).hide();
  } );

  function artPreviewBody() {
    var content = this.getContent();
    if ( $( '.show-cover', '.article-edit-details' )[ 0 ].checked ) {
      content = require( '../../lib/utils' ).coverHtmlBuild() + content;
    }
    $( this.body ).mediaSrcAttr();

    if ( combiner.$selector.hasClass( 'active' ) ) {
      $( "#art_preview_body", combiner.$iframe ).html( content ).mediaFormat().mediaSrcAttr();
    }
  }
  ue.addListener( 'blur', function() {

    editData.graphDataDetails[ editData.editIndex ].content = this.getContent();

  } );
  // 编辑器内容改变
  ue.addListener( 'contentChange', function() {
    var content = this.getContent();

    artPreviewBody.call( this );

    if ( editData.isCreate ) {
      AutoSave.start();
      editData.setAttrbute( 'isEdit', 1 );
    } else {
      if ( editData.allIsReady && editData.firstContentChange ) {
        AutoSave.start();
        editData.setAttrbute( 'isEdit', 1 );
      }
    }

    editData.firstContentChange = 1;
    editData.isContentChange = true;
    editData.graphDataDetails[ editData.editIndex ].content = this.getContent();
  } );

  // 
  ue.addListener( 'afterSelectionChange', function() {
    // beta
    //editData.setAttrbute('content', this.getContent());
    editData.graphDataDetails[ editData.editIndex ].content = this.getContent();
    /*if (editData.ueeditorAndEditDataIsReady) {
    	editData.isContentChange = true;
    }*/
  } );

  // 用户开始使用键盘输入内容
  ue.addListener( 'keydown', function() {
    AutoSave.start();
  } );
  // 手动增加点击事件
  /*$('.edit-column2').on('click', '.edui-for-source .edui-button-body', function() {
  	$('.edit-input.title-input').trigger('keydown');
  });*/
  $.extend( combiner, {
    hide: function() {
      $( 'iframe', combiner.$iframe ).removeAttr( 'src' );
    },
    show: function() {
      artPreviewBody.call( ue );
    }
  } );

} );